package org.eae.eldf.webapp.controller.base;


import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eae.eldf.webapp.base.AbstractController;
import org.eae.eldf.webapp.vo.PageListVo;
import org.eae.eldf.entity.base.BaseCompanyInfo;
import org.eae.eldf.entity.base.BaseCompanyLinkman;
import org.eae.eldf.entity.base.BaseParameter;
import org.eae.eldf.framework.annotation.Token;
import org.eae.eldf.framework.pager.Pagination;
import org.eae.eldf.framework.persistence.Criteria;
import org.eae.eldf.service.base.BaseCompanyInfoService;
import org.eae.eldf.service.base.BaseCompanyLinkmanService;
import org.eae.eldf.service.base.BaseParameterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
/**
 * ****************************************************************************
 * @File name   :      BaseParameterController.java
 *
 * @Author      :      qubibo
 *
 * @Date        :      2016-6-7
 *
 * @Copyright Notice: 
 * Copyright (c) 2016 Efast Serv. All  Rights Reserved.
 * This software is published under the terms of the Efast Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------
 * Date                   Who         Version        Comments
 * 2016-6-7 上午10:55:48        qubibo     1.0            Initial Version
 ****************************************************************************
 */
@Controller
@RequestMapping("/base")
public class BaseParameterController extends AbstractController<BaseParameter>{
	
	private BaseParameterService baseParameterService;
	
	private BaseCompanyInfoService baseCompanyInfoService;
	
	private BaseCompanyLinkmanService baseCompanyLinkmanService;
	
	

	@Autowired
	public void setBaseParameterService(BaseParameterService baseParameterService) {
		this.baseParameterService = baseParameterService;
	}
	
	@Autowired
	public void setBaseCompanyInfoService(
			BaseCompanyInfoService baseCompanyInfoService) {
		this.baseCompanyInfoService = baseCompanyInfoService;
	}


	@Autowired
	public void setBaseCompanyLinkmanService(
			BaseCompanyLinkmanService baseCompanyLinkmanService) {
		this.baseCompanyLinkmanService = baseCompanyLinkmanService;
	}

	/**
	 * 跳转到list页面
	 * @Date        :      2016-6-7
	 * @param map
	 * @param searchValue
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/parameter/list")
	public String listPage(ModelMap map,@RequestParam(required=false)String searchValue,HttpServletRequest request,HttpServletResponse response){
	
		return "/base/parameter/list";
	}
	/**
	 * list页面异步请求
	 * @Date        :      2016-6-7
	 * @param request
	 * @param response
	 * @param page
	 * @param rows
	 * @param searchValue
	 * @return
	 */
	@RequestMapping(value = "/parameter/list_json")
	public @ResponseBody PageListVo<BaseParameter> pageList(
			HttpServletRequest request, 
			HttpServletResponse response,
			@RequestParam(required = false) Integer page,
			@RequestParam(required = false) Integer rows,
			@RequestParam(required = false) String searchValue){
		Pagination<BaseParameter> pager = super.initPagination(request);
		pager = baseParameterService.queryListBaseParameter(pager, searchValue);
		return pageListJson(pager);
	}
	
	

	/**
	 * 跳转到参数页面
	 * @param modelMap
	 * @param id
	 * @param searchValue
	 * @return
	 */
	@RequestMapping(value="/parameter/form")
	@Token(save=true)
	public String toCompanyPage(ModelMap modelMap,@RequestParam(required=false) Long id,
			@RequestParam(required=false) String searchValue){
		BaseParameter baseParameter=null;
		if(id!=null){
			baseParameter=baseParameterService.get(BaseParameter.class, id);
		} else {
			baseParameter=new BaseParameter();
		}
		List<BaseCompanyInfo> baseCompanyInfoList = baseCompanyInfoService.queryList(Criteria.create(BaseCompanyInfo.class));
		System.out.println(baseCompanyInfoList.size());
		modelMap.addAttribute("baseCompanyInfoList",baseCompanyInfoList);
		modelMap.addAttribute("baseParameter",baseParameter);
		modelMap.addAttribute("searchValue",searchValue);
		return "/base/parameter/form";

	}
	
	/**
	 * 新增参数
	 * @Date        :      2016-6-7
	 * @param modelAndView
	 * @param moduleName
	 * @param paramKey
	 * @param i18nParamKey
	 * @param paramDesc
	 * @param i18nDesc
	 * @param paramScope
	 * @param paramLevel
	 * @param dataType
	 * @param paramValue
	 * @param valid
	 * @param modelMap
	 * @param request
	 * @return
	 */
	@RequestMapping("/parameter/insert")
	@Token(remove=true,repeatSubmitForwardURL="/base/parameter/form")
	public ModelAndView insert(ModelAndView modelAndView,
		
			@ModelAttribute BaseParameter baseParameter,
			ModelMap modelMap,HttpServletRequest request){
			
			modelAndView.setViewName("forward:/base/parameter/form");
			
			baseParameter.setId(baseParameter.getEnterpriseId()+"."+baseParameter.getUserId()+"."+baseParameter.getModuleName()+"."+baseParameter.getParamKey());
			baseParameter.setI18nParamKey(baseParameter.getModuleName()+"."+baseParameter.getParamKey());
			baseParameter.setI18nDesc(baseParameter.getModuleName()+"."+baseParameter.getParamKey());
			baseParameterService.insert(baseParameter);
			modelAndView.addObject("success", true);
		return modelAndView;

	}

	
	
	/**
	 * 修改参数
	 * @Date        :      2016-6-14
	 * @param id
	 * @param modelAndView
	 * @param baseParameter
	 * @param modelMap
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/parameter/edit")
	public ModelAndView update(@RequestParam(required=false) Long id,
			ModelAndView modelAndView,
			@ModelAttribute BaseParameter baseParameter,
			ModelMap modelMap,HttpServletRequest request){
		modelAndView.setViewName("forward:/base/parameter/form");
		//baseParameter.setId(baseParameter.getEnterpriseId()+"."+baseParameter.getUserId()+"."+baseParameter.getModuleName()+"."+baseParameter.getParamKey());
		/*baseParameter.setI18nParamKey(baseParameter.getModuleName()+"."+baseParameter.getParamKey());*/
		//baseParameter.setI18nDesc(baseParameter.getModuleName()+"."+baseParameter.getParamKey());
		baseParameterService.update(Criteria.create(BaseParameter.class).set("enterpriseId", baseParameter.getEnterpriseId())
				.set("userId", baseParameter.getUserId())
				.set("moduleName", baseParameter.getModuleName())
				.set("paramKey", baseParameter.getParamKey())
				.set("i18nParamKey",baseParameter.getModuleName()+"."+baseParameter.getParamKey())
				.set("paramDesc", baseParameter.getParamDesc())
				.set("i18nDesc", baseParameter.getModuleName()+"."+baseParameter.getParamKey())
				.set("id", baseParameter.getEnterpriseId()+"."+baseParameter.getUserId()+"."+baseParameter.getModuleName()+"."+baseParameter.getParamKey())
				.set("paramScope", baseParameter.getParamScope())
				.set("paramLevel", baseParameter.getParamLevel())
				.set("dataType", baseParameter.getDataType())
				.set("paramValue", baseParameter.getParamValue())
				.set("valid", baseParameter.getValid())
				.where("baseParameterId", new Object[]{id})
				);
		modelAndView.addObject("success",true);
		return modelAndView;
	}
	
	/**
	 * 删除参数   把valid从0设为1
	 * @Date        :      2016-6-7
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/parameter/del")
	public @ResponseBody String delete(@RequestParam Long id){
		try {
			baseParameterService.del(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "success";
	}
	
	/**
	 * 异步加载公司人员名单
	 * @Date        :      2016-6-14
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/parameter/linkman")
	public @ResponseBody
	List<BaseCompanyLinkman> findLinkmanByCompanyCode(ModelMap modelMap ,@RequestParam String companyCode) {
		
		List<BaseCompanyLinkman> listBaseCompanyLinkmans = baseCompanyLinkmanService.findLinkmanByCompanyCode(companyCode);
		modelMap.addAttribute("listBaseCompanyLinkmans",listBaseCompanyLinkmans);
		System.out.println("list"+listBaseCompanyLinkmans.size());
		return listBaseCompanyLinkmans;
	}
}
